<?php

class myRememberMeFilter extends sfFilter
{
  public function execute ($filterChain)
  {
    $cookie = $this->getContext()->getRequest()->getCookie( sfConfig::get( 'app_sf_guard_plugin_remember_cookie_name' ) );
    // execute this filter only once, and if the user is not already logged in, and has a cookie set
    if ( $this->isFirstCall()
         && !$this->getContext()->getUser()->isAuthenticated()
         && $cookie )
    {
      // See if a user exists with this cookie in the remember database
      $user = Doctrine_Query::create()
              ->from( 'sfGuardUser u' )
              ->leftJoin( 'sfGuardRememberKey k' )
              ->where( 'k.remember_key', $cookie )
              ->andWhere( 'k.ip_address', $this->getContext()->getRequest()->getRemoteAddress() )
              ->limit( 1 )
              ->execute();
      if ( $user )
      {
        $this->getContext()->getUser()->signIn( $user );
      }
    }
    // execute next filter
    $filterChain->execute();
  }
}